$(function () {
    initTaskGrid();
});

function doSearch() {
    $('#dg').datagrid('load', {
        'pubKey': $("#param_pubKey").val(),
        'groupId': $("#param_groupId").val()
    });
}

function refreshGrid() {
    $('#dg').datagrid('reload');
}

function initTaskGrid() {
    $('#dg').datagrid({
        url: '/admin/permission/list',
        fit: true,
        pageSize: 10,
        pagination: true,
        pageList: [10, 30, 45],
        remoteSort: true,
        sortName: 'updateTime',
        sortOrder: 'desc',
        rownumbers: true,
        singleSelect: true,

        columns: [[
            {field: 'id', hidden: true},
            {field: 'groupName', width: 100, title: '联盟分组', sortable: true, align: 'center'},
            {field: 'publicKey', width: 400, title: 'pubKey', sortable: true, align: 'center'},
            {field: 'tableName', width: 100, title: '表名', sortable: true, align: 'center'},
            {field: 'permissionTypeDesc', width: 150, title: '权限', sortable: true, align: 'center'},
            {
                field: 'opt', width: 300, title: '操作', sortable: false,
                formatter: function (value, row) {
                    var result = "<button type=\"button\" class=\"btn btn-info btn-sm\" onclick='javascript:updatePermission(" + row.id + ")'>修改</button>";
                    result += "&nbsp;&nbsp;<button type=\"button\" class=\"btn btn-danger btn-sm\" onclick='javascript:delPermission(" + row.id + ")'>删除</button>";
                    return result;
                }
            }
        ]],
        onLoadSuccess: function (data) {
            $("#dg").datagrid("clearSelections");
        }
    });
}

function delPermission(id) {
    layer.confirm("确定删除？", {icon: 3, title: '提示'}, function (index) {
        $.ajax({
            url: "/admin/permission/del?id=" + id,
            type: "GET",
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            success: function (data) {
                var obj = eval(data);
                var msg = obj.message;
                if (obj.code == '200') {
                    refreshGrid();
                } else {
                    layer.msg(msg, {icon: 5});
                }
            }
        });
        layer.close(index);
    });
}

function addPermission() {
    layer.open({
        type: 2,
        title: '新增用户',
        shade: 0.8,
        maxmin: true,
        area: ['70%', '90%'],
        skin: 'layui-layer-rim', //加上边框
        content: ["/admin/permission/addPage", 'yes'],
        btn: ['保存', '关闭'],
        yes: function (index, layero) {
            layer.getChildFrame("#btn_sub").click();
        }
    });
}

function updatePermission(id) {
    layer.open({
        type: 2,
        title: '修改',
        shade: 0.8,
        maxmin: true,
        area: ['70%', '60%'],
        skin: 'layui-layer-rim', //加上边框
        content: ["/admin/permission/updatePage?id=" + id, 'yes'],
        btn: ['保存', '关闭']
        , yes: function (index, layero) {
            layer.getChildFrame("#btn_sub").click();
        }
    });
}